package ctxSemanticsGen;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;

public class ClassProcessor {
	
	ClassStructure cs;
	
	
	/**
	 * Creates a Class Processor aimed to create the aspects implementing the 
	 * COP semantics. The class for which the aspects are generated is described
	 * by the class structure cs.
	 * 
	 * @param cs
	 */
	public ClassProcessor(ClassStructure cs){
		this.cs = cs;
	}
	
	
	/**
	 * Generates the aspects implementing the COP sematics for the class
	 * represented by this classprocessor. The aspects is generated at the 
	 * given path. E.g."./out" 
	 * 
	 * @param path
	 * @throws ClassNotFoundException
	 */
	public void generateAspects(String path) throws ClassNotFoundException{
		
		// TODO: add other checks.
		/* Sanity checks for the class. HERE !? */
		Class c = cs.getDescribedClass();
		if (!c.isAnnotationPresent(cop.publ.LayeredClass.class)) {
			throw new RuntimeException("Class does not declare the " +
					                   "LayeredClass annotation!");
		}

		
		/* Create the aspects */
		for (Method m : cs.getAdvicedMethods()){
			Collection<Method> lm = cs.getLayeredMethodsForAdvicedMethod(m);
			
			AspectGenerator ag =  new AspectGenerator();
			ag.setClassStructure(cs);
			ag.setAdvicedMethod(m);
			ag.setLayeredMethods(lm);
			ag.generateAspect(path);
		}
		
	}	
}
